﻿<!-- Views/Admin/CreateQuestion.cshtml -->
@model WebApplication1.Models.Question

@{
    ViewBag.Title = "添加试题";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<div class="container">
    <h2>添加试题</h2>
    <h4>@ViewBag.ExamName</h4>
    <hr />

    @using (Html.BeginForm())
    {
        @Html.AntiForgeryToken()

        @Html.HiddenFor(model => model.ExamId)

        <div class="form-horizontal">
            @Html.ValidationSummary(true, "", new { @class = "text-danger" })

            <div class="form-group">
                @Html.LabelFor(model => model.QuestionText, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.TextAreaFor(model => model.QuestionText, new { @class = "form-control", rows = 4, placeholder = "请输入试题内容" })
                    @Html.ValidationMessageFor(model => model.QuestionText, "", new { @class = "text-danger" })
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.QuestionType, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.DropDownListFor(model => model.QuestionType, new SelectList(new List<SelectListItem>
                       {
                           new SelectListItem { Text = "单选题", Value = "Single" },
                           new SelectListItem { Text = "多选题", Value = "Multiple" },
                           new SelectListItem { Text = "判断题", Value = "TrueFalse" },
                           new SelectListItem { Text = "填空题", Value = "Fill" }
                       }, "Value", "Text"), "请选择试题类型", new { @class = "form-control" })

                    @Html.ValidationMessageFor(model => model.QuestionType, "", new { @class = "text-danger" })
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.Score, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.Score, new { htmlAttributes = new { @class = "form-control", placeholder = "请输入分数" } })
                    @Html.ValidationMessageFor(model => model.Score, "", new { @class = "text-danger" })
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.CorrectAnswer, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.CorrectAnswer, new { htmlAttributes = new { @class = "form-control", placeholder = "请输入正确答案" } })
                    @Html.ValidationMessageFor(model => model.CorrectAnswer, "", new { @class = "text-danger" })
                    <span class="help-block">
                        单选题：输入正确选项标识（如A）<br />
                        多选题：输入正确选项标识，多个用逗号分隔（如A,B,C）<br />
                        判断题：输入True或False<br />
                        填空题：输入正确选项标识，如有多个空，用逗号分隔（如A,B,C）
                    </span>
                </div>
            </div>

            <div class="form-group">
                <div class="col-md-offset-2 col-md-10">
                    <input type="submit" value="添加试题" class="btn btn-primary" />
                    <a href="@Url.Action("Questions", "Admin", new { examId = Model.ExamId })" class="btn btn-danger">返回列表</a>
                </div>
            </div>
        </div>
    }
</div>

@section Scripts {

    <script>
        $(document).ready(function () {
            // 设置默认分数
            if ($('#Score').val() === '') {
                $('#Score').val(10);
            }

            // 表单提交前验证
            $('form').submit(function () {
                var questionText = $('#QuestionText').val();
                var questionType = $('#QuestionType').val();
                var score = $('#Score').val();
                var correctAnswer = $('#CorrectAnswer').val();

                if (!questionText.trim()) {
                    alert('请输入试题内容');
                    return false;
                }

                if (!questionType) {
                    alert('请选择试题类型');
                    return false;
                }

                if (!score || score <= 0) {
                    alert('请输入有效的分数');
                    return false;
                }

                if (!correctAnswer.trim()) {
                    alert('请输入正确答案');
                    return false;
                }

                return true;
            });
        });
    </script>
}